package com.showcai.controller.admin;

import com.showcai.controller.base.BaseController;
import com.showcai.module.Role;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.service.AdministratorService;
import com.showcai.service.MenuService;
import com.showcai.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/2/26.
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController extends BaseController {

    @Autowired
    AdministratorService administratorService;
  @Autowired
  MenuService menuService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value="/getMenuListForRole", method = RequestMethod.POST)
    @ResponseBody
    public String getMenuListForRole(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters();
            resultObj.setData(menuService.selectMenuListForRole(map));
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }

    @RequestMapping(value="/role")
    @ResponseBody
    public ModelAndView Role(HttpServletRequest request) {
        ModelAndView m = new ModelAndView("/role/RoleAdd");
        Map<String,String> map =this.showRequestParameters();
        if(map.get("tid")!=null){
            Role role = roleService.selectRole(map);
            m.addObject("role",role);
        }
        return  m;
    }

    @RequestMapping(value="/addRole" , method = RequestMethod.POST)
    @ResponseBody
    public String addRole(Role role) {
        ResultObj resultObj = new ResultObj();
        try {
            roleService.addRole(role);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }

    @RequestMapping(value="/saveMenuRole" , method = RequestMethod.POST)
    @ResponseBody
    public String saveMenuRole(HttpServletRequest request) {
        System.out.println("=================================saveMenuRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters();

            resultObj.setMsg("成功！");
            roleService.modifyRole(map);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }
  @RequestMapping(value="/deleteMenu" , method = RequestMethod.POST)
    @ResponseBody
    public String deleteMenu() {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters();

            resultObj.setMsg("成功！");
            menuService.deleteMenu(map);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }



    @RequestMapping(value="/getRoleList")
    public ModelAndView getRoleList() {
        System.out.println("=============================getRoleList=============================================>>>>>>");
        ModelAndView mov=new ModelAndView();
        mov.setViewName("/role/getRoleList");

        Map<String,String> map=this.showRequestParameters();
        List<Role> roleList = roleService.getRoleList();
        mov.addObject("bean",map);
        mov.addObject("roleList",roleList);
        return mov;

    }





}
